package com.glorysoft.xztpda.ui.batchtrackin

import androidx.databinding.ObservableArrayList
import com.glorysoft.xztpda.entity.ui.ToastMessageBean
import com.glorysoft.xztpda.BR
import com.glorysoft.xztpda.R
import com.glorysoft.xztpda.base.BaseViewModel
import com.glorysoft.xztpda.entity.LotInfoEntity
import com.glorysoft.xztpda.entity.LotList
import com.glorysoft.xztpda.model.LotRepository
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import me.tatarka.bindingcollectionadapter2.ItemBinding
import java.util.ArrayList

/**
 * FileName: BatchTrackInViewModel
 * Author: XJH
 * Date: 2022/4/13 16:48
 * Description:
 * History:
 */
class BatchTrackInViewModel : BaseViewModel() {
    private val repository: LotRepository = LotRepository()

    val callback: DeleteListener = object : DeleteListener {
        override fun onLotDelete(item: LotInfoEntity) {
            lotItems.remove(item)
        }

    }

    var lotItems = ObservableArrayList<LotInfoEntity>()
    var lotBinding =
        ItemBinding.of<LotInfoEntity>(BR.lotItems, R.layout.item_batch_track_in)
            .bindExtra(BR.onTrackInDelete, callback)

    fun getLotInfo(s: String) {
        for (entity in lotItems) {
            if (entity.lot.lotid.equals(s)) return
        }

        val map = JsonObject()
        map.addProperty("ACTIONTYPE","LOTINFOBYID")
        map.addProperty("LOTID",s)
        map.addProperty("EQUIPMENTID","")
        launchOnlyResult({
            val lotInfo = repository.getLotInfo(map)
            if (!lotInfo.lot.state.equals("等待作业")) {
                defUI.toastEvent.value = ToastMessageBean("批次状态不允许",false)
                return@launchOnlyResult
            }

            if (lotInfo.lot.holdstate.equals("On")) {
                defUI.toastEvent.value = ToastMessageBean("批次状态不允许",false)
                return@launchOnlyResult
            }

            if (lotItems.size <= 0) {
                lotItems.add(lotInfo)
            } else {
                if (lotItems[0].lot.stepname == lotInfo.lot.stepname
                ) {
                    if (lotItems[0].lot.state ==(lotInfo.lot.state)
                    ) {
                        lotItems.add(lotInfo)
                    } else {
                        defUI.toastEvent.value = ToastMessageBean("扫描批次状态与列表批次状态不一致，无法添加.",false)
                    }
                    return@launchOnlyResult
                }
                defUI.toastEvent.value = ToastMessageBean("扫描批次工序与列表批次工序不一致，无法添加.",false)
            }

        })

    }

    fun batchTrackIn(toString: String) {
        val map = JsonObject()
        map.addProperty("ACTIONTYPE", "DefaultTrackIn")
        map.addProperty("EQUIPMENTID", toString)
        map.addProperty("ISCHECKSTEPAUTHORITY", false)
        val lotList: MutableList<LotList> = ArrayList<LotList>()
        for (entity in lotItems) {
            lotList.add(LotList(entity.lot.lotid))
        }
        map.add("LOTLIST", JsonParser().parse(Gson().toJson(lotList)).asJsonArray) //传批次集合
        launchOnlyResult({
            repository.trackIn(map)
            defUI.startActivity.call()
        })


    }

    fun checkEqp(eqp: String) {
        val map = JsonObject()
        map.addProperty("ACTIONTYPE", "CAPACHECK")
        map.addProperty("LOTID", lotItems[0].lot.lotid)
        map.addProperty("EQUIPMENTID", eqp)
        launchOnlyResult({
            repository.checkEqp(map)
            batchTrackIn(eqp)
        })

    }


    interface DeleteListener {
        fun onLotDelete(item: LotInfoEntity)
    }

}